var xmlHttp; // global instance of XMLHttpRequest

function createXmlHttpRequest(){
    if (window.ActiveXObject) {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest){
        xmlHttp=new XMLHttpRequest();
    }
}
/**************************************************************************/
function changeDay() {
    
    var month=document.getElementById('selMonth').value;
    
    var year=parseInt(document.getElementById('selYear').value);

    if (month == '2' || month == '4' || month == '6' || month == '9' || month == '11') 
        document.getElementById('day31').disabled=true;
    else
        document.getElementById('day31').disabled = false;

    if (month == '2') {
        document.getElementById('day30').disabled=true;
        if (year % 4 != 0) 
            document.getElementById('day29').disabled=true;
        else 
            document.getElementById('day29').disabled=false;
    }
    else document.getElementById('day30').disabled=false;
}
/**************************************************************************/
function changeccDay() {
    
    var month = document.getElementById('selCreditCardMonth').value;

    var year = document.getElementById('selCreditCardYear').value;

    if (month == '2' || month == '4' || month == '6' || month == '9' || month == '11') 
        document.getElementById('ccday31').disabled=true;
    else 
        document.getElementById('ccday31').disabled=false ;

    if (month == '2') {
        document.getElementById('ccday30').disabled=true;
        if (year%4 != 0) document.getElementById('ccday29').disabled=true;
            else document.getElementById('ccday29').disabled=false;
    }
    else document.getElementById('ccday30').disabled=false; 
}
/**************************************************************************/
function checkform(){

    var err1 = document.getElementById('lblEmailError').innerHTML; 
    var err2=document.getElementById('lblPasswordError').innerHTML;
    var err3=document.getElementById('lblRetypePasswordError').innerHTML;
    var err4=document.getElementById('lblFirstNameError').innerHTML;
    var err5=document.getElementById('lblLastNameError').innerHTML;
   
    if (err1.length !=  0) {alert(err1); return false;}
    if (err2.length !=  0) {alert(err2); return false; }
    if (err3.length !=  0) {alert(err3); return false; }
    if (err4.length !=  0) {alert(err4); return false; }
    if (err5.length !=  0) {alert(err5); return false; }
   
   var year=document.getElementById('selYear').value; 
   var month=document.getElementById('selMonth').value;
   var day=document.getElementById('selDay').value;
   var yearInt = parseInt(year);
   
   if (year=='0' || month=='0'|| day=='0') {alert("Birthday is required. "); return false;}
   if (((month=='2' || month=='4'||month=='6'||month=='9'||month=='11')&&(day=='31')) || (month=='2' && day=='30') || (month=='2' && day=='29' &&year%4!=0 ) ) {alert("Birthday seems not valid."); return false;}
   year=document.getElementById('ccyear').value;
   month=document.getElementById('ccmonth').value;
   day=document.getElementById('ccday').value;
   yearInt=parseInt(year);
   if (year=='0' || month=='0'|| day=='0') {alert("Credit Card expiry date is required. ");  return false;}
   if (((month=='2' || month=='4'||month=='6'||month=='9'||month=='11')&&(day=='31')) || (month=='2' && day=='30') || (month=='2' && day=='29' &&year%4!=0 ) ) {alert("Credit Card expiry Date seems not valid."); return false;}
 
 return true;
}
/**************************************************************************/
function checkemail() {   
    
    var emailStr = document.getElementById('email').value;

    if (emailStr.length == 0) {
        changelabel('lblEmailError', "Required");
        return;
    }
    return;
}
/**************************************************************************/
function ajaxCheckEmailExist() {
    
    createXmlHttpRequest();
    
    var strEmail = document.getElementById('email').value;
    var strQuery = "txtEmail=" + strEmail;
    xmlHttp.open("POST", "ValidateRegisterServlet", true);
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(strQuery);
}
/**************************************************************************/
function handleStateChange(){
    
    if(xmlHttp.readyState==4){
        if(xmlHttp.status==200){
            var nodError = xmlHttp.responseXML.getElementsByTagName("EXIST");
            document.getElementById("emailErr").innerHTML= nodError[0].firstChild.nodeValue;
        }
        else{
            alert("Error loading pagen"+ xmlHttp.status +":"+ xmlHttp.statusText);
        }
    }
}
/**************************************************************************/
function checkccn() {

    var ccnStr = document.getElementById('txtCreditCardNumber').value;
}
/**************************************************************************/
function checkpw(pw) {
    
    var pwStr = document.getElementById(pw).value;
    if (pwStr.length >= 6 && pwStr.length <= 10) {
        document.getElementById('imgPasswordStrengt').style.display = 'inline';
        document.getElementById('imgPasswordStrengt').src = './images/passWeak.png';
        return;
    }
    if (pwStr.length > 10) {
        document.getElementById('imgPasswordStrengt').style.display = 'inline';
        document.getElementById('imgPasswordStrengt').src = './images/passStrong.png';
        return;
    }
}
/**************************************************************************/
function checkpwv() {
    
    var pwStr= document.getElementById('txtPassword').value;
    if (pwStr.length == 0) {
        changelabel('lblPasswordError',"Password required");
        document.getElementById('imgPasswordStrength').style.display = 'none';
        return;
    }
    if (pwStr.length < 6) {
        changelabel('lblPasswordError',"Too few characters");
        document.getElementById('imgPasswordStrength').style.display = 'none';
        return;
    }
}
/**************************************************************************/
function checkrpw(rd, pw) {
    
    var rdStr=document.getElementById(rd).value;
    var pwStr = document.getElementById(pw).value;
    if (rdStr.length == 0) {
        changelabel('lblPasswordError', "Password required");
        return;
    }
    if (rdStr != pwStr) 
        changelabel('lblRetypePasswordError',"Password mismatch.");
}
/**************************************************************************/
function checkfn() {
    
    var fnStr=document.getElementById('txtFirstName').value;
    if (fnStr.length == 0) {
        changelabel('fnErr', "First name required");
        return;
    }
}
/**************************************************************************/
function checkln() {
    
    var lnStr=document.getElementById('txtLastName').value;
    if (lnStr.length == 0) {
        changelabel('lnErr', "Last name required");
        return;
    }
}
/**************************************************************************/
function changelabel(label, text) {
    document.getElementById(label).innerHTML = text;
}
/**************************************************************************/